import request from '@/utils/request'

const api = {
    dbList: '/tool/database/page',
    dbAdd: '/tool/database',
    dbPut: '/tool/database',
    dbDel: '/tool/database',
    dbTableList: 'tool/gen/table/info/page',
    dbSelectData:'/tool/database/select/data',
    previewCode: '/tool/gen/preview/code',
    generateCode: 'tool/gen/code',
    templateGroupSelectAll:'/tool/tpl/group/all',
    templateGroupList: '/tool/tpl/group/page',
    templateInfos: '/tool/tpl/info/list',
    templatePropertyList: '/tool/tpl/property/list'

}

export default api

export function getDbList (parameter) {
    return request({
        url: api.dbList,
        method: 'get',
        params: parameter
    })
}

/**
 * 获取数据库下拉数据
 * @param parameter
 */
export function getDbSelectData () {
    return request({
        url: api.dbSelectData,
        method: 'get'
    })
}

/**
 * 得到所有的模板分组 下拉
 */
export function getAllTemplateGroupSelect () {
    return request({
        url: api.templateGroupSelectAll,
        method: 'get'
    })
}

/**
 * 得到所有模板组
 */
export function getAllTemplateGroup (parameter) {
    return request({
        url: api.templateGroupList,
        method: 'get',
        params: parameter
    })
}


// id == 0 add     post
// id != 0 update  put
export function saveDataSource (parameter) {
    return request({
        url: api.dbAdd,
        method:  'post' ,
        data: parameter
    })
}


export function putDataSource (parameter) {
    return request({
        url: api.dbPut,
        method:  'put',
        data: parameter
    })
}


export function delDataSource (id) {
    return request({
        url: `${api.dbPut}/${id}`,
        method:  'delete'
    })
}

/**
 * 获取模板信息 根据群组id
 * @param parameter
 */
export function getTemplateInfosByGid (parameter) {
    return request({
        url: `${api.templateInfos}/${parameter}`,
        method: 'get',
    })
}

/**
 * 获取模板属性列表
 * @param parameter
 */
export function getTemplatePropertiesByGid (parameter) {
    return request({
        url: `${api.templatePropertyList}/${parameter}`,
        method: 'get',
    })
}

export function getTableList (dsName,parameter) {
    return request({
        url: api.dbTableList,
        method: 'get',
        params: parameter,
        headers:{'dsName': dsName}
    })
}

/**
 * {
  "customProperties": {
    "additionalProp1": "string",
    "additionalProp2": "string",
    "additionalProp3": "string"
  },
  "tableName": "string",
  "tablePrefix": "string",
  "templateFileIds": [
    0
  ],
  "templateGroupId": 0
}
 * @param parameter
 */
export function previewCode (dsName,parameter) {
    return request({
        url: api.previewCode,
        method:  'post' ,
        data: parameter,
        headers:{'dsName': dsName,"Content-Type":"application/json"}
    })
}
export function generateCode(dsName,parameter) {
    return request({
        url: api.generateCode,
        method:  'post' ,
        data: parameter,
        responseType: 'blob',
        headers:{'dsName': dsName,"Content-Type":"application/json"}
    })
}


